package com.heima.controller;

import com.heima.pojo.dto.OrderPageDto;
import com.heima.pojo.vo.OrderPageVo;
import com.heima.pojo.vo.SkuVo;
import com.heima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索
     */
    @GetMapping("/api/order-service/order/search")
    public OrderPageVo pageQuery(OrderPageDto dto) {
        log.info("dto:{}", dto);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            if (StringUtils.hasText(dto.getEndDate()) && StringUtils.hasText(dto.getStartDate())) {
                first = simpleDateFormat.parse(dto.getStartDate());
                last = simpleDateFormat.parse(dto.getEndDate());
            }
        } catch (ParseException e) {
            log.info("日期信息转换失败", e);
        }
        dto.setStart(first);
        dto.setEnd(last);
        return orderService.pageQuery(dto);
    }

    /**
     *获取售货机商品销量
     */
    @GetMapping("/api/order-service/report/skuCollect/{innerCode}/{start}/{end}")
    public List<SkuVo> skuCollect(@PathVariable String end, @PathVariable String innerCode, @PathVariable String start){
        log.info("end:{},start:{},innerCode:{}",end,start,innerCode);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        List<SkuVo> skuVos = orderService.skuCollect(innerCode,first,last);
        return skuVos;
    }
}
